package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.User;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order-release-stock")
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void OrderClose(OrderEntity orderEntity, Message msg,Channel channel) {
        //关闭订单
        orderService.OrderClose(orderEntity);
    }

    //手动ack 必须手动签收 要不然队列消息一直存中
    @RabbitHandler
    public void getTest(Message message, Channel channel, User user) {
        System.out.println("接受消息");
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("姓名:"+user.getName()+"年龄"+user.getAge());
        try {
            if (deliveryTag%2==0){
                //b:是否批量签收
                channel.basicAck(deliveryTag,false);
            }else {
                System.out.println("丢失");
                //拒签 重新入队
                channel.basicNack(deliveryTag,false,true);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
